package com.bat.conf;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @Description: bean使用全定义类名
 * @Author: zpf
 * @Date: 2019/11/14 下午3:04
 */

public class UniqueNameGenerator extends AnnotationBeanNameGenerator {

    @Override
    @NonNull
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        //如果配置就走配置
        if (definition instanceof AnnotatedBeanDefinition) {
            String beanName = this.determineBeanNameFromAnnotation((AnnotatedBeanDefinition)definition);
            if (StringUtils.hasText(beanName)) {
                return beanName;
            }
        }
        //全限定类名
        return Objects.requireNonNull(definition.getBeanClassName());
    }
}
